//
//  ReadFolderViewController.m
//  我的图书馆
//
//  Created by 孙  路 on 15-1-12.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ReadFolderViewController.h"
//调用数据库
#import "DetailBace.h"
//使用libraryCell
#import "LibraryTableViewCell.h"
//使用EditModel
#import "DetailModel.h"
//移动信息进入其他文件夹
#import "ListViewController.h"
#define kBlueColor [UIColor colorWithRed:129/255.0 green: 198/255.0 blue: 221/255.0 alpha: 1.0]
@interface ReadFolderViewController ()

@end

@implementation ReadFolderViewController
- (void)dealloc
{
    [_tableArray release];
    [_tableV release];
    [_FolderName release];
    [_targetFolder release];

    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];

        self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49) style:UITableViewStyleGrouped];
        [_tableV registerClass:[LibraryTableViewCell class] forCellReuseIdentifier:@"ReadCustomCell"];
        _tableV.delegate = self;
        _tableV.dataSource = self;
        [self.view addSubview:_tableV];

        [_tableV release];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   // [self readFolderNameFormDataBace];
    [self createTabBar];
}
#pragma mark -
#pragma mark 读取数据库
- (void)readFolderNameFormDataBace
{
    [_tableArray removeAllObjects];
    //_tableArray = [DetailBace searchAllEditByFolderName:_FolderName];
    [_tableV reloadData];
}
- (void)createTabBar
{
    UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 49, self.view.frame.size.width, 49)];
    [bView setBackgroundColor:kBlueColor];

    UIButton *moveButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [moveButton setFrame:CGRectMake(30, 10, 60, 29)];
    [moveButton addTarget:self action:@selector(moveCell:) forControlEvents:UIControlEventTouchUpInside];
    [moveButton setTitle:@"移动" forState:UIControlStateNormal];
    [moveButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [bView addSubview:moveButton];

    UIButton *removeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [removeButton setFrame:CGRectMake(self.view.frame.size.width - 140, 10, 60, 29)];
    [removeButton addTarget:self action:@selector(removeCell:) forControlEvents:UIControlEventTouchUpInside];
    [removeButton setTitle:@"删除" forState:UIControlStateNormal];
    [removeButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [bView addSubview:removeButton];

    [self.view addSubview:bView];
    [bView release];

}
/*
 1. 改变单例为NO
 2. 移动信息, 从当前文件夹取出
 3. 存到目标文件夹中
 4. 删除该信息
 */
- (void)moveCell:(id)sender
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:NO forKey:@"targetFolder"];

    [self.navigationController popViewControllerAnimated:YES];

    DetailModel *model = [_tableArray objectAtIndex:_indexPathRow];

    [DetailBace insertIntoDetailWithDetailModel:model byFolderName:_targetFolder];

    [DetailBace deleteFromDetailWithDetail_id:_indexPathRow];

    [_tableV reloadData];
}
- (void)removeCell:(id)sender//删除信息
{
    [DetailBace deleteFromDetailWithDetail_id:_indexPathRow];
}
#pragma mark -
#pragma mark 点击获取下标(进入详情)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _indexPathRow = indexPath.row;

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"进行文件编辑还是进入详情" delegate:self cancelButtonTitle:@"文件编辑" otherButtonTitles:@"进入详情", nil];
    alert.delegate = self;
    [alert show];
    [alert release];

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {

        // 1. 进入详情

    }
}
#pragma mark -
#pragma mark 编辑Cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath//显示Cell
{
    LibraryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReadCustomCell"];
    if ([_tableArray count] != 0) {
        DetailModel *detail = [_tableArray objectAtIndex:indexPath.row];
        cell.titleLabel.text = detail.detail_tit;
        //cell.authorLabel.text = detail.SNName;
        [_tableV reloadData];
        return cell;
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section//自定义section
{
    UIView *aView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)] autorelease];
    [aView setBackgroundColor:[UIColor orangeColor]];

    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 30, 30)];
    [imageV setImage:[UIImage imageNamed:@"iconfont-iconfontwenjianjia.png"]];
    [aView addSubview:imageV];
    [imageV release];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 5, 200, 30)];
    [label setText:_FolderName];//文件夹名字
    [aView addSubview:label];
    [label release];

    return aView;
}
#pragma mark -
#pragma mark 滑动删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath == 0) {
        return UITableViewCellEditingStyleInsert;
    }else
        return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_tableArray removeObjectAtIndex:indexPath.row];
        [_tableV reloadData];
    }else if (editingStyle == UITableViewCellEditingStyleInsert){
        [_tableArray addObject:@"1"];
        [_tableV reloadData];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
